package killerqueen.game.objects;

import killerqueen.game.interfaces.GameObject;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;

public class RotatingCircle implements GameObject {

	private CircleShape cs;
	private Body body;

	private float minAngularSpeed;
	private float maxAngularSpeed;
	private float angularIncrease;
	private float radius;

	private float x;

	private float y;

	public RotatingCircle() {
		// Empty
	}

	@Override
	public void init(World world, String id) {
		BodyDef bodyDef = new BodyDef();
		bodyDef.position.set(x, y);
		bodyDef.type = BodyType.KINEMATIC;
		body = world.createBody(bodyDef);

		cs = new CircleShape();
		cs.m_radius = radius;

		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.shape = cs;
		fixtureDef.density = 5;
		fixtureDef.friction = 1f;
		fixtureDef.restitution = 0.1f;

		body.createFixture(fixtureDef);
		body.setAngularVelocity(minAngularSpeed);
	}

	@Override
	public void input(Input input) {
		// Empty
	}

	@Override
	public void paint(Graphics g) {
		g.pushTransform();

		Vec2 pos = body.getPosition();
		float angle = body.getAngle();
		g.translate(pos.x, pos.y);

		g.rotate(0, 0, (float) Math.toDegrees(+angle));
		org.newdawn.slick.geom.Circle circle = new org.newdawn.slick.geom.Circle(
				0, 0, cs.m_radius);

		g.setColor(Color.blue);
		g.fill(circle);
		g.setColor(Color.black);
		g.drawLine(0, 0, cs.m_radius, 0);
		g.draw(circle);

		g.popTransform();
	}

	@Override
	public void update(long delta) {
		if (body.getAngularVelocity() < minAngularSpeed
				|| body.getAngularVelocity() > maxAngularSpeed){
			angularIncrease = -angularIncrease;
		}
		body.setAngularVelocity(body.getAngularVelocity()
				+ body.getAngularVelocity() * angularIncrease);
	}

	public void setAngularIncrease(float angularIncrease) {
		this.angularIncrease = angularIncrease;
	}

	public void setMinAngularSpeed(float minAngularSpeed) {
		this.minAngularSpeed = minAngularSpeed;
	}

	public void setMaxAngularSpeed(float maxAngularSpeed) {
		this.maxAngularSpeed = maxAngularSpeed;
	}

	public void setRadius(float radius) {
		this.radius = radius;
	}

	public void setX(float x) {
		this.x = x;
	}

	public void setY(float y) {
		this.y = y;
	}

	@Override
	public void destroy(World world) {
		// TODO Auto-generated method stub
		
	}
}
